#-*-coding:utf-8 -*-
import os

def read_index_file(file_path):
    type_dict={'spam':'1','ham':'0'}
    index_file=open(file_path)
    index_dict={}
    try:
        for line in index_file:
            arr=line.split(' ')
            if len(arr)==2:
                key,value=arr
            # 添加到字段中
            value=value.replace('../data','').replace('\n','')
            index_dict[value]=type_dict[key.lower()]

    finally:
            index_file.close()

    return index_dict

# 邮件的文件内容数据读取
def read_file(file_path):
    file=open(file_path,'r',encoding='gb2312',errors='ignore')
    content_dict={}

    try:
        is_content=False # 初始化为False后，在循环之外
        for line in file:
            line=line.strip()
            if line.startswith('From:'):
                content_dict['from']=line[5:]
            elif line.startswith('To:'):
                content_dict['to']=line[3:]
            elif line.startswith('Date:'):
                content_dict['date']=line[5:]
            elif not line:
                is_content=True

            # 处理邮件内容
            if is_content:
                if 'content' in content_dict:
                    content_dict['content']+=line
                else:
                    content_dict['content']=line
    finally:
        file.close()

    return content_dict

# 邮件数据处理
def process_file(file_path):
    content_dict=read_file(file_path)

    # 进行处理
    result_str=content_dict.get('from','unknow').replace(',','').strip()+","
    result_str+=content_dict.get('to','unknow').replace(',','').strip()+","
    result_str+=content_dict.get('date','unknow').replace(',','').strip()+","
    result_str+=content_dict.get('content','unknow').replace(',','').strip()

    return result_str

#使用函数开始数据处理
def data_process():
    doc_path = '../../doc/'
    index_dict=read_index_file(doc_path+'full/index')
    dir_list=os.listdir(doc_path+'data/data')
    for dir_item in dir_list: # 开始把N个文件夹中的file写入N*n个wiriter
        dir_path=doc_path+'data/data/'+dir_item
        print('开始处理文件夹：'+dir_path)
        file_list=os.listdir(dir_path)
        write_file_path=doc_path+'data/process/process_'+dir_item
        
        with open(write_file_path,'w',encoding='utf-8') as writer:
            for file_item in file_list:
                file_path=dir_path+'/'+file_item  #得到要处理文件的具体路径
                index_key='/'+dir_item+'/'+file_item
                if index_key in index_dict:
                    content_str=process_file(file_path)
                    content_str+=','+index_dict[index_key]+'\n'
                    writer.writelines(content_str)

    with open(doc_path+'data/result/process_result_01','w',encoding='utf-8') as writer:
        for dir_item in dir_list:
            file_path=doc_path+'data/process/process_'+dir_item
            print('开始合并文件：'+file_path)

            with open(file_path,encoding='utf-8') as file:
                for line in file:
                    writer.writelines(line)
                print(writer)


data_process()
